package com.itts.hk.map;


import lombok.Data;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * 地图坐标转化
 */
@Data
public class GPSConverterUtils {
    public static final String BAIDU_LBS_TYPE = "bd09ll";
    public static double pi = 3.1415926535897932384626;
    public static double a = 6378245.0;
    public static double ee = 0.00669342162296594323;

    /**
     * 火星坐标系 (GCJ-02) 坐标转换成 百度坐标系 (BD-09) 的转换算法
     *
     * @param gg_lat
     * @param gg_lon
     * @return
     */
    public static GpsBO gcj02_To_Bd09(double gg_lat, double gg_lon) {
        double x = gg_lon, y = gg_lat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * pi);
        double bd_lon = z * Math.cos(theta) + 0.0065;
        double bd_lat = z * Math.sin(theta) + 0.006;
        return new GpsBO(bd_lon, bd_lat);
    }

    /**
     * 百度坐标系 (BD-09) 坐标转换成火星坐标系 (GCJ-02) 的转换算法
     *
     * @param bd_lat
     * @param bd_lon
     * @return
     */
    public static GpsBO bd09_To_Gcj02(double bd_lat, double bd_lon) {
        double x = bd_lon - 0.0065, y = bd_lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * pi);
        double gg_lon = z * Math.cos(theta);
        double gg_lat = z * Math.sin(theta);
        return new GpsBO(gg_lon,gg_lat);
    }

    //批量转化坐标为火星坐标
    public static List<GpsBO> getGps(List<GpsBO> list) {
        List<GpsBO> gpsBOS = new ArrayList<>();
        for (GpsBO gpsBO : list) {
            GpsBO gps = gcj02_To_Bd09(gpsBO.getLon(), gpsBO.getLat());
            gpsBOS.add(gps);
        }
        return gpsBOS;
    }

    @Test
    public void test() {
        double a = 31.585743;
        double b = 120.324466;
        //GpsBO bo = new GpsBO(120.324466, 31.585743);
//        GpsBO gpsBO = bd09_To_Gcj02(a, b);
//        List<GpsBO> gpsBOS = new ArrayList<>();
//        gpsBOS.add(bo);
//        List<GpsBO> gps = getGps(gpsBOS);
        System.out.println(bd09_To_Gcj02(a,b));
    }



    //120.217157,31.603567
    @Test
    public void test2(){
        GpsBO gpsBO = bd09_To_Gcj02(31.603567,120.217157);//百度坐标
        System.out.println(gpsBO);    //高德坐标lon=120.2107502219114, lat=31.597287348883203
    }

}
